/*
    Copyright (c) 2007-2016 Pentair Technical Products, Inc.
    All rights reserved.
    Pentair Technical Products, Inc. proprietary and confidential.

    Description:
	This header file contains common declarations
	for application-level modules.

    $Revision: 13617 $
*/

#ifndef __APPDEFS_H__
#define __APPDEFS_H__

#include <linux/string.h>
#include <semaphore.h>

#include "ring.h"

#define I2c_SLAVE_ADDR	0x10

#define MAX_ACTIVATE_CNT	3
/*
 * case 0x886:		//I2C_SLAVE_MODE
 *
 * case 0x888:		//I2C_MASTER_MODE
 *
 * case 0x887:		//I2C_SET_RING
 *
 * case 0x889:		//I2C_GET_RING
 */
#define	I2C_REINIT			0x885
#define	I2C_SLAVE_MODE		0x886
#define	I2C_MASTER_MODE		0x888
#define	I2C_SET_RING		0x887
#define	I2C_GET_RING		0x889

#define IPMB_REQUEST_RETRY_COUNT	10 /* how many times to retry request */
#define IPMB_EVENT_RETRY_COUNT		10 /* how many times retry event */

/* Storage class for IPMI message size */
typedef unsigned short msg_len_t;

/* broadcast flag */
#define MSG_BCAST   (1 << 4)

/* Storage class for IPMI message size */
typedef unsigned short msg_len_t;

/* Command handler return type */
typedef short cmd_ret_t;




#define fanSpeed(w)		(w/100)

#pragma  pack(1)
/* IPMI/PICMG/SIPL message header */
typedef struct msg_header {

    unsigned char srcif;
    unsigned char session_idx;

    unsigned char rsLUN:2;
    unsigned char netFn:6;

    unsigned char rsSA;
    unsigned char rqSA;

    unsigned char rqSeq;
    unsigned char rqLUN;

    unsigned char cmd;

    msg_len_t length;

    unsigned char data[0];
} msg_header_t;


typedef struct {
    unsigned char rsSA;
    unsigned char rsLUN:2;
    unsigned char netFn:6;
    unsigned char crc;

    unsigned char rqSA;
    unsigned char rqLUN:2;
    unsigned char rqSeq:6;
    unsigned char cmd;
    unsigned char data[0];
} ipmb_msg_header_t;

typedef struct msg_fru {
    unsigned char device_id;
    unsigned char device_ver;

    unsigned char firmware_ver1;
    unsigned char firmware_ver2;

    unsigned char ipmi_ver;
    unsigned char device_sup;

    unsigned char manufacture_id_h;
    unsigned short manufacture_id_l;

    unsigned short product_id;

    unsigned char aux_firmware_h;
    unsigned short aux_firmware_l;
} msg_fru_t;

#pragma  pack()

/* Routine for detecting duplicated messages */
static inline unsigned char msg_header_cmp(msg_header_t *msg1,
	msg_header_t *msg2)
{
	return !memcmp((char *)msg1 + 1, (char *)msg2 + 1, sizeof(*msg1) - 1);
    //return !memcmp((void *)msg1 + 1, (void *)msg2 + 1, sizeof(*msg1) - 1);
}


#define APP_OK			0
#define APP_INVAL		-1
#define APP_TIMEOUT		-2
#define APP_BUSY		-3
#define APP_NACK		-4
#define APP_NOTREADY		-5
#define APP_ERROR		-6
#define APP_TRACKBUSY		-7

/** Type to hold APP_* error codes. Can not be used to return length
    information. */
typedef char  app_err_t;

/** Type to hold APP_* error codes AND return length information. */
typedef short app_err_len_t;

/** Macro to test the value of both app_err_t and app_err_len_t to determine
    successfull operation. */
#define APP_SUCCESS(x)		(x >= 0)

//------------------------channel

/**
    Types of IPMI interfaces supported by the firmware
*/
typedef enum {
    IPMI_IFACE_NONE,
    IPMI_IFACE_SDI,
    IPMI_IFACE_PI,
    IPMI_IFACE_IPMB,
    IPMI_IFACE_BTI,
    IPMI_IFACE_KCS,
    IPMI_IFACE_BT,
    IPMI_IFACE_IPMBL,
    IPMI_IFACE_IPMBL2,
    IPMI_IFACE_IPMBV,
    IPMI_IFACE_LAN,
    IPMI_IFACE_LAN1 = IPMI_IFACE_LAN,
} ipmi_iface_types_t;

#define offsetof(type,memb) ((size_t)&((type *)0)->memb)
/*
    FRU state
*/

#define FRU_STATE(x)			((x) & 0x0f)
#define FRU_STATE_CHANGE_CAUSE(x)	((x) & 0xf0)
#define FRU_STATE_M0			(0 << 0)
#define FRU_STATE_M1			(1 << 0)
#define FRU_STATE_M2			(2 << 0)
#define FRU_STATE_M3			(3 << 0)
#define FRU_STATE_M4			(4 << 0)
#define FRU_STATE_M5			(5 << 0)
#define FRU_STATE_M6			(6 << 0)
#define FRU_STATE_M7			(7 << 0)

#define FRU_STATE_CHANGE_NORMAL                        (0 << 4)
#define FRU_STATE_CHANGE_SHMC_COMMAND                  (1 << 4)
#define FRU_STATE_CHANGE_EJECTOR                       (2 << 4)
#define FRU_STATE_CHANGE_PROGRAMMATIC                  (3 << 4)
#define FRU_COMMUNICATION_LOST                         (4 << 4)
#define FRU_COMMUNICATION_LOST_LOCAL_FAILURE           (5 << 4)
#define FRU_SURPRISE_STATE_CHANGE_EXTRACTION           (6 << 4)
#define FRU_STATE_CHANGE_INFORMATION                   (7 << 4)
#define FRU_INVALID_HARDWARE_ADDRESS_DETECTED          (8 << 4)
#define FRU_UNEXPECTED_DEACTIVATION                    (9 << 4)
#define FRU_SURPRISE_STATE_CHANGE_POWER_FAILURE        (10 << 4)
#define FRU_STATE_CHANGE_CAUSE_UNKNOWN                 (15 << 4)

#ifdef CFG_VPX_MODULE
/* Remap PICMG 3.0 codes that reserved in VITA 46.11 */
#undef FRU_SURPRISE_STATE_CHANGE_POWER_FAILURE
#define FRU_SURPRISE_STATE_CHANGE_POWER_FAILURE		\
    FRU_UNEXPECTED_DEACTIVATION
#undef FRU_STATE_CHANGE_EJECTOR
#define FRU_STATE_CHANGE_EJECTOR		FRU_STATE_CHANGE_CAUSE_UNKNOWN
#endif



#define APP_OK			0
#define APP_INVAL		-1
#define APP_TIMEOUT		-2
#define APP_BUSY		-3
#define APP_NACK		-4
#define APP_NOTREADY		-5
#define APP_ERROR		-6
#define APP_TRACKBUSY		-7

/** Type to hold APP_* error codes. Can not be used to return length
    information. */
typedef char  app_err_t;

/** Type to hold APP_* error codes AND return length information. */
typedef short app_err_len_t;

/** Macro to test the value of both app_err_t and app_err_len_t to determine
    successfull operation. */
#define APP_SUCCESS(x)		(x >= 0)

#define CONSOLE_CMD_HEAD	0xA
#define CONSOLE_CMD_TAIL	0xF
#define CONSOLE_CMD_IPMI	0x12



extern int g_i2c_file;
extern char *topicname[];

extern void *net_sendbit(void *arg);
extern void *admin_func(void *arg);
extern void SlectUartMenu(void);
extern unsigned int reverse_bit(unsigned int n,int length);
extern void *net_sendheart(void *arg);
extern void *manageMainStandby(void *arg);
extern int i2c_change_mode(int file,unsigned char ms_sl);
extern void MacAddrInit(unsigned int chasisNum,unsigned int slotNum);
extern int i2c_init();
extern void iface_ipmb_process_i2c_ring();
extern int netInit();
extern int GetIP(int id, unsigned char *ipaddr);
extern void app_process_main_ring(void);
extern int i2c_master_send(int file,unsigned char addr,unsigned char *buf,unsigned int len);
extern void SetFruActivationCmd(unsigned char slotNum,char activate);
extern void ddsPublishInit();
extern void adminSemInit();
extern void netSemInit();
extern app_err_t app_send_reply(msg_header_t *header,unsigned char	*data,msg_len_t	len);
extern void format_ipmb_message(ipmb_msg_header_t * ipmb_msg, unsigned char rsSA,
		unsigned char netFn, unsigned char rsLUN, unsigned char rqSA,
		unsigned char rqSeq, unsigned char rqLUN, unsigned char cmd,
		unsigned char *data, msg_len_t len);
extern int ipmb_send_message(int file,unsigned char addr,unsigned char *buf,unsigned int len);
extern void ControlFruActivation();
extern char cmd_handle(msg_header_t *header);

extern void main_ring_set_data_ptr(ring_size_t off);
extern void main_ring_get_buf(void *buf, ring_size_t count);
extern ring_ptr_t main_ring_get_data_ptr(ring_size_t off);

extern ring_t app_main_ring;
extern unsigned short main_ring_rx;

#endif
